package com.ktjy.ulits;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class StringDateConverter implements Converter<String, Date> {

    private List<String> patterns;

    public StringDateConverter(List<String> patterns) {
        this.patterns = patterns;
    }

    @Override
    public Date convert(String s) {
        for (String pattern : patterns) {
            try {
                SimpleDateFormat sf = new SimpleDateFormat(pattern);
                return sf.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
                continue;
            }
        }
        return null;
    }

}
